package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.ActivityManagerUtils;
import com.example.wanbaqian.utils.BaseUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.tencent.bugly.beta.Beta;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.OnClick;
import ch.ielse.view.SwitchView;

/**
 * 设置界面
 */
public class SettingActivity extends BaseActivity implements HttpCallback {

    @BindView(R.id.setting_about)
    AutoRelativeLayout settingAbout;
    @BindView(R.id.setting_message)
    AutoRelativeLayout settingMessage;
    @BindView(R.id.setting_function)
    AutoRelativeLayout settingFunction;
    @BindView(R.id.setting_version)
    AutoRelativeLayout settingVersion;
    @BindView(R.id.setting_versionCode)
    TextView settingVersionCode;
    @BindView(R.id.setting_exitLogin)
    Button settingExitLogin;
    @BindView(R.id.setting_Switch)
    SwitchView setting_Switch;
    private static final String TAG = "SettingActivity";

    @Override
    protected int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.colorBackground);
        setToolBar("设置");
        settingVersionCode.setText("V" + BaseUtils.geVersionCode(mContext));
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.USER_SET, mContext,
                new String[]{"accessid"},
                new String[]{SPUtils.getAccessid(mContext)}), HttpURL.USER_SET, CommonValue.REQUEST_CODE1, this);
    }

    @OnClick({R.id.setting_about, R.id.setting_exitLogin, R.id.setting_message, R.id.setting_function, R.id.setting_version, R.id.setting_Switch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_about:
                Bundle bundle = new Bundle();
                bundle.putString("title", ""+getResources().getString(R.string.app_wxjjs));
                bundle.putString("url", "http://www.lanmeibank.com/intor_lanmei.html");
                openActivity(WebViewActivity.class, bundle);
                break;
            case R.id.setting_exitLogin:
                HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.USER_LOGOUT, mContext,
                        new String[]{"accessid"}, new String[]{SPUtils.getAccessid(mContext)})
                        , mContext, null, CommonValue.REQUEST_CODE3, this);
                break;
            case R.id.setting_message:
                break;
            case R.id.setting_function:
                openActivity(FeedbackActivity.class);
                break;
            case R.id.setting_version:
                Beta.checkUpgrade();
                break;
            case R.id.setting_Switch:
                HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.PUSH_SET, mContext,
                        new String[]{"accessid"},
                        new String[]{SPUtils.getAccessid(mContext)}), mContext, null, CommonValue.REQUEST_CODE2, this);
                break;
        }
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                setting_Switch.toggleSwitch(content.equals("1") ? true : false);
                break;
            case CommonValue.REQUEST_CODE2:
                showToast("设置成功");
                break;
            case CommonValue.REQUEST_CODE3:
                SPUtils.CleanUserInfo(mContext);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ActivityManagerUtils.getInstance().exit();
                    }
                }, 300);
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {
    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
